/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

// DlgMsg.cpp : implementation file

#include "stdafx.h"
#include "CHScanner.h"
#include "DlgMsg.h"
#include ".\dlgmsg.h"

#define MAX_MESSAGE_COUNT 100

//---------------------------------------------------------------------------

IMPLEMENT_DYNAMIC(DlgMsg, CRgnDialog)
DlgMsg::DlgMsg(CWnd* pParent /*=NULL*/) : CRgnDialog(DlgMsg::IDD, pParent)
{
	maxMsgCnt = MAX_MESSAGE_COUNT;
}

DlgMsg::DlgMsg(int maxMsgCntIn, CWnd* pParent /*=NULL*/) : CRgnDialog(DlgMsg::IDD, pParent)
{
	maxMsgCnt = maxMsgCntIn;
	if(maxMsgCnt<=0)
		maxMsgCnt = MAX_MESSAGE_COUNT;
}

DlgMsg::~DlgMsg()
{
}

//---------------------------------------------------------------------------

void DlgMsg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_MSG_LIST, messageList);
}

//---------------------------------------------------------------------------

BEGIN_MESSAGE_MAP(DlgMsg, CRgnDialog)
END_MESSAGE_MAP()

//---------------------------------------------------------------------------

void DlgMsg::ResizeChilds(void)
{
	messageList.MoveWindow(clientRect.left, clientRect.top, clientRect.right - clientRect.left, clientRect.bottom - clientRect.top, TRUE);
}

//---------------------------------------------------------------------------

void DlgMsg::ScrollAsNeeded(void)
{
	int i, cnt;
	int height, itemHeight, totalHeight;
	int clientHeight;
	RECT rect;

	itemHeight  = 0;
	totalHeight = 0;
	cnt = messageList.GetCount();
	for(i=0; i<cnt; i++){
		height = messageList.GetItemHeight(i);
		if(height!=LB_ERR){
			if(itemHeight < height)
				itemHeight = height;
			totalHeight += height;
		}
	}

	messageList.GetClientRect(&rect);
	clientHeight = rect.bottom - rect.top;

	if(clientHeight < totalHeight){
		// scroll needed
		height = totalHeight - clientHeight;
		height /= itemHeight;
		height ++;
		messageList.SetTopIndex(height);
	}
}

bool DlgMsg::AddMessage(CString strIn)
{
	int res;

	while(messageList.GetCount()>=MAX_MESSAGE_COUNT)
		messageList.DeleteString(0);

	res = messageList.AddString(strIn);
	if(res==LB_ERR) return false;
	if(res==LB_ERRSPACE) return false;

	ScrollAsNeeded();

	messageList.Invalidate();
	messageList.UpdateWindow();

	return true;
}

//---------------------------------------------------------------------------

void DlgMsg::OnOK()
{
	// CRgnDialog::OnOK();
}

void DlgMsg::OnCancel()
{

	// CRgnDialog::OnCancel();
}

//---------------------------------------------------------------------------
